package com.fneuch.w40k;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.RedirectView;

import com.fneuch.game.minis.AttackResult;
import com.fneuch.game.minis.GameEngine;
import com.fneuch.game.minis.ShootingEngine;
import com.fneuch.game.minis.w40k.army.Unit;

@Controller
@RequestMapping("/mathammer")
public class MathammerController {
	
	
	@Autowired
	private GameEngine gameEngine;
	
	
	public MathammerController() {
		super();
		attaquants = new HashMap<String, Unit>();
		defenders = new HashMap<String, Unit>();
		lastResult = new AttackResult();
	}

	private Map<String, Unit> attaquants;
	private Map<String, Unit> defenders;
	private AttackResult lastResult;
	
	@RequestMapping
    public ModelAndView get() {
        ModelAndView mav = new ModelAndView();
        mav.setViewName("mathammer");
//        List<Unit> unitesAttaquants = new ArrayList<Unit>();
//        unitesAttaquants.addAll(attaquants.values());
//        mav.addObject("attaquants", unitesAttaquants);
//        List<Unit> unitesDefendantes = new ArrayList<Unit>();
//        unitesDefendantes.addAll(defenders.values());
        //mav.addObject("defenders", unitesDefendantes);
        if(attaquants.values().size() > 0 && defenders.values().size()>0) {
    		executeShooting();
            //mav.addObject("attackResult", lastResult);
        }
        mav.addObject("attaquant", new Unit());
        mav.addObject("defender", new Unit());
        return mav;
    }
	
	@ModelAttribute("attaquants")
	public List<Unit> getAttaquants() {
		List<Unit> unitesAttaquants = new ArrayList<Unit>();
		unitesAttaquants.addAll(attaquants.values());
		return unitesAttaquants;
	}

	public void setAttaquants(Map<String, Unit> attaquants) {
		this.attaquants = attaquants;
	}

	@ModelAttribute("defenders")
	public List<Unit> getDefenders() {
      List<Unit> unitesDefendantes = new ArrayList<Unit>();
      unitesDefendantes.addAll(defenders.values());
		return unitesDefendantes;
	}

	public void setDefenders(Map<String, Unit> defenders) {
		this.defenders = defenders;
	}

	@ModelAttribute("attackResult")
	public AttackResult getLastResult() {
		return lastResult;
	}

	public void setLastResult(AttackResult lastResult) {
		this.lastResult = lastResult;
	}

	@RequestMapping(value="/attaquants", method=RequestMethod.POST)
	public String addAttaquant(@Valid @ModelAttribute("attaquant") Unit unite, BindingResult result) {
		if (!result.hasErrors()) {
            this.attaquants = new HashMap<String, Unit>();
		    this.attaquants.put(unite.getId(), unite);
		} 
		return "redirect:/mathammer";
	}
	@RequestMapping(value="/attaquants/{uniteId}", method=RequestMethod.DELETE)
	public String removeAttaquant(@PathVariable String uniteId, Model model) {
		this.attaquants = new HashMap<String, Unit>();
	    return "redirect:/mathammer";
	}
	
	@RequestMapping(value="/defenders", method=RequestMethod.POST)
	public ModelAndView addDefender(@Valid@ModelAttribute("defender") Unit unite, BindingResult result) {
		ModelAndView mav; 
		if (!result.hasErrors()) {
			this.defenders = new HashMap<String, Unit>();
			this.defenders.put(unite.getId(), unite);
	        RedirectView view = new RedirectView("/mathammer");
	        mav = new ModelAndView(view);
		} else {
			mav = new ModelAndView();
	        mav.setViewName("mathammer");
		}
		 
	    return mav;
	}
	
	@RequestMapping(value="/defenders/{uniteId}", method=RequestMethod.DELETE)
	public String removeDefender(@PathVariable String uniteId, Model model) {
		this.defenders = new HashMap<String, Unit>();
	    return "redirect:/mathammer";
	}

	@RequestMapping(value="/shoot", method=RequestMethod.POST)
	public String shoot(BindingResult result) {
		executeShooting();	
	    return "redirect:/mathammer";
	}

	private void executeShooting() {
		ShootingEngine shooting = gameEngine.getShootingEngine();
		Unit shooter = attaquants.get(attaquants.keySet().iterator().next());
		Unit defender = defenders.get(defenders.keySet().iterator().next());
		lastResult = shooting.shoot(shooter.getNbModels(), shooter, defender.getNbModels(), defender);
	}

	public void setGameEngine(GameEngine gameEngine) {
		this.gameEngine = gameEngine;
	}

	public GameEngine getGameEngine() {
		return gameEngine;
	}

}
